package com.gitee.wsl.common.commonmark

import org.commonmark.node.Block
import org.commonmark.node.Node

object NodeExt {

}

fun Node.replaceFirst(other:Node){
    val that=this
    var removeNode:Node?=null
    this.firstChild?.let {
        that.insertBefore(it)
        removeNode = it
        it.unlink()
    }
    if( removeNode == null){
        appendChild(other)
    }
}

fun Node.replaceLast(other:Node){
    val that=this
    var removeNode:Node?=null
    this.lastChild?.let {
        that.insertAfter(it)
        removeNode = it
        it.unlink()
    }
    if( removeNode == null){
        appendChild(other)
    }
}

fun Node.replace(other:Node){
    insertBefore(other)
    unlink()
}

fun Block.copyFrom(other:Block){
    var node = other.lastChild
    while(node!=null){
        appendChild(node)
        node = node.lastChild
    }
    sourceSpans = other.sourceSpans
}